package com.groupseven.happycampus.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupseven.happycampus.common.bo.CardPayLogBo;
import com.groupseven.happycampus.common.pojo.CardPayLog;
import com.groupseven.happycampus.common.utils.BeanUtils;
import com.groupseven.happycampus.common.vo.LayuiPageVo;
import com.groupseven.happycampus.mapper.CardPayLogMapper;
import com.groupseven.happycampus.service.CardPayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @BeLongsProject: happycampus
 * @BeLongsPackage: com.groupseven.happycampus.service.impl
 * @Author: WRH
 * @CreateTime: 2022-07-28 16:03
 * @Description: TODO
 * @Version: 1.0
 */
@Service("cardPayLogService")
public class CardPayLogServiceImpl implements CardPayLogService {
    @Autowired
    private CardPayLogMapper cardPayLogMapper;
    @Override
    public Boolean insertCardPayLog(Map<String, Object> params) {
        CardPayLog cardPayLog = new CardPayLog();
        // 使用 BeanUtils 工具类将 Map 中的数据注入到 Card 中
        BeanUtils.copyOf(params, cardPayLog);

        Integer result = cardPayLogMapper.insertCardPayLog(cardPayLog);

        return result != 0;
    }

    @Override
    public Boolean deleteCardPayLog(Map<String, Object> params) {
        CardPayLog cardPayLog = new CardPayLog();
        // 使用 BeanUtils 工具类将 Map 中的数据注入到 Card 中
        BeanUtils.copyOf(params, cardPayLog);

        Integer result = cardPayLogMapper.deleteCardPayLog(cardPayLog);

        return result != 0;
    }

    @Override
    public LayuiPageVo<CardPayLog> cardPayLogList(Map<String, Object> params) {
        int page = Integer.parseInt((String) params.get("page"));
        int limit = Integer.parseInt((String) params.get("limit"));

        PageHelper.startPage(page,limit);

        List<CardPayLog> cardPayLogList = cardPayLogMapper.cardPayLogList(params);

        PageInfo<CardPayLog> cardPayLogPageInfo = new PageInfo<>(cardPayLogList);

        List<CardPayLog> list = cardPayLogPageInfo.getList();

        List<CardPayLog> cardPayLogs = list.stream().map(o -> {
            if ("0".equals(o.getPayType())) {
                o.setPayType("食堂");
            }
            if ("1".equals(o.getPayType())) {
                o.setPayType("超市");
            }
            if ("2".equals(o.getPayType())) {
                o.setPayType("跑腿");
            }
            return o;
        }).collect(Collectors.toList());

        return new LayuiPageVo<CardPayLog>(0,"卡号数据",cardPayLogPageInfo.getTotal(),cardPayLogs);
    }

    @Override
    public Boolean updateCardPayLog(Map<String, Object> params) {
        CardPayLog cardPayLog = new CardPayLog();
        // 使用 BeanUtils 工具类将 Map 中的数据注入到 Card 中
        BeanUtils.copyOf(params, cardPayLog);

        Integer result = cardPayLogMapper.updateCardPayLog(cardPayLog);

        return result != 0;
    }

    @Override
    public boolean deleteCardPayLogsByIds(String ids) {
        List<Integer> list = Arrays.stream(ids.split(",")).map(o -> Integer.parseInt(o)).collect(Collectors.toList());
        Integer result = cardPayLogMapper.deleteCardPayLogsByIds(list);
        return result >0;
    }

    @Override
    public List<CardPayLogBo> cardPayLogExcelData(CardPayLog cardPayLog) {
        List<CardPayLog> cardPayLogs = cardPayLogMapper.selectCardPayLogs(cardPayLog);
        List<CardPayLogBo> cardPayLogBos = cardPayLogs.stream()
                .map(o -> new CardPayLogBo(o.getPayId(),o.getPayDate(),o.getPayType(),o.getPayMoney(),o.getCardId()))
                .collect(Collectors.toList());

        return cardPayLogBos;
    }

    @Override
    public void addCardPayLogs(CardPayLog cardPayLog) {
        cardPayLogMapper.addCardPayLogs(cardPayLog);
    }
}
